package playing;

import object.Plant;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class showplants {
    private int userId;

    public showplants(int userId) {
        this.userId = userId;

        JFrame frame = new JFrame("我的植物");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JLabel titleLabel = new JLabel("这是你现在的植物");
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(titleLabel);
        panel.add(Box.createVerticalStrut(10));

        ArrayList<Plant> nowPlants = new ArrayList<>();
        List<Integer> nowPlantIds = new PlayerPlantReading().getPlantIdsByUserId(userId);
        for (int id : nowPlantIds) {
            nowPlants.add(new PlantDataReader().getPlantById(id));
        }

        if (!nowPlants.isEmpty()) {
            for (int i = 0; i < nowPlants.size(); i++) {
                JLabel plantLabel = new JLabel((i + 1) + "号植物: " + nowPlants.get(i).toString());
                plantLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
                panel.add(plantLabel);
            }
        } else {
            JLabel noPlantLabel = new JLabel("你现在还没有一个植物，快来主菜单领养吧");
            noPlantLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(noPlantLabel);
        }

        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new showplants(1); // 假设用户ID为1
            }
        });
    }
}
